package com.song.util;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CodeGenerate {


    public static void main(String[] arg) {

        final String projectPath="D:\\myCode\\loginFormwork\\user\\src\\main\\";
//        String projectPath=onePath;

        AutoGenerator ag = new AutoGenerator();

        //1. 全局配置

        GlobalConfig config = new GlobalConfig();

        config.setAuthor("Song") //作者

                .setOutputDir(projectPath+"java")  //生成路径

                .setFileOverride(true)//是否文件覆盖，如果多次

                .setIdType(IdType.AUTO) //主键策略

                .setServiceName("%sService") //设置生成的service接口名首字母不用I开头

                .setBaseResultMap(true)//映射文件中生成默认的baseMap

                .setBaseColumnList(true);//映射文件中生成默认的基础列名sql

        //2. 数据源配置

        DataSourceConfig dsConfig = new DataSourceConfig();

        dsConfig.setDbType(DbType.MYSQL)

                .setUrl("jdbc:mysql://localhost:3306/analyse?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true")

                .setDriverName("com.mysql.jdbc.Driver")

                .setUsername("root")

                .setPassword("123456");

        //3.策略配置

        StrategyConfig stConfig = new StrategyConfig();

        stConfig.setCapitalMode(true) // 全局大写命名

                .setNaming(NamingStrategy.underline_to_camel) //下划线转驼峰
//                .setInclude(new String[]{"lexicon","lexicon_type_mapping","xiang_type","xiang_cognition","cognition_factor","cognition_answers","hierarchy"})//生成的表,参数为数组
                .setInclude(new String[]{"tuser","menu"})//xiang_pronunciation
                .setTablePrefix("xiang_");                       //去除表名前缀
        //4.包名策略

        PackageConfig pkConfig = new PackageConfig();

        pkConfig.setParent("com.song")//父包名

                .setController("controller")

                .setEntity("domain")

                .setService("service")

                .setMapper("mappers");



        //5.生成xml的配置

        List<FileOutConfig> focList = new ArrayList<>();

        // 自定义配置

        InjectionConfig cfg = new InjectionConfig() {

            @Override

            public void initMap() {

            }

        };

        String templatePath = "/templates/mapper.xml.vm";

        // 自定义配置会被优先输出

        focList.add(new FileOutConfig(templatePath) {

            @Override

            public String outputFile(TableInfo tableInfo) {

                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！

                return projectPath+"\\resources\\mappers\\"+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;

            }

        });

        cfg.setFileOutConfigList(focList);

        // 配置模板

        TemplateConfig templateConfig = new TemplateConfig();

        //不生成如下类型模板

        templateConfig.setXml(null);

        ag.setTemplate(templateConfig);

        //6.整合配置

        ag.setGlobalConfig(config)

                .setDataSource(dsConfig)

                .setStrategy(stConfig)

                .setPackageInfo(pkConfig)

                .setCfg(cfg)

                .setTemplateEngine(new VelocityTemplateEngine());

        ag.execute();
    }

}
